package com.gjt.URI_URL;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created with Intel IDEA.
 *
 * 把uri中例如 &a=99&ip=133 转换成看到的样子
 * @Auther: gjt
 * @Date: 2024/3/22 20:40
 */
public class QueryString {
    private StringBuilder query = new StringBuilder();

    public QueryString() {
    }
    public synchronized void add(String name, String value){
        query.append('&');
        encode(name,value);
    }

    private synchronized void encode(String name, String value){
        try{
            query.append(URLEncoder.encode(name,"UTF-8"));
            query.append('=');
            query.append(URLEncoder.encode(value,"UTF-8"));
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
    }

    public synchronized String getQuery(){
        return query.toString();
    }

    @Override
    public String toString() {
        return getQuery();
    }
}
